/*
 * AfterSaleProgressLayout.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.yidiandao.business.utils.SysUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/** 售后进度/订单进度 */
public class AfterSaleProgressLayout extends ViewGroup {

    private Context context;
    /** 水平间隔,默认15dp */
    private int mHorSpacing = UIUtils.dp2px(0);

    private int mChildHeight;

    public AfterSaleProgressLayout(Context context) {
        this(context, null);
    }

    public AfterSaleProgressLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();

        // 开始计算左边位置 需要加上paddingLeft
        int childLeft = paddingLeft;
        View childView;
        for (int i = 0; i < getChildCount(); i++) {
            childView = getChildAt(i);
            // 获取Child的宽度
            int childWidth = SysUtils.getScreenWidth(context) / getChildCount();
            measureChildren(
                    MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY),
                    heightMeasureSpec);
            mChildHeight = childView.getMeasuredHeight() + paddingTop + paddingBottom;

            childLeft = childLeft + childWidth + mHorSpacing;
        }

        // 最后加上paddingRight
        int widthSize = childLeft;

        setMeasuredDimension(widthSize, MeasureSpec.getSize(mChildHeight));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();

        int childLeft = paddingLeft;
        int childTop = paddingTop;
        int childBottom = paddingBottom;

        View childView;
        for (int i = 0; i < getChildCount(); i++) {
            childView = getChildAt(i);

            int childWidth = childView.getMeasuredWidth();
            int childHeight = childView.getMeasuredHeight();

            childView.layout(
                    childLeft,
                    childTop,
                    childLeft + childWidth,
                    childHeight + childBottom + childTop);

            childLeft = childWidth + childLeft + mHorSpacing;
        }
    }

    /** 给Layout设置数据 */
    public void setData(String state, List<String> status) {
        removeAllViews();
        if (status != null && status.size() > 0) {
            for (int i = 0; i < status.size(); i++) {
                AfterSaleProgressView view = new AfterSaleProgressView(context);
                view.setData(state, i, status);
                addView(view);
            }
        }
    }
}
